<?php

namespace Beerus\Modules\Designer\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Doraemon\model\Designer as DesignerModel;
use Doraemon\model\Designer\DesignerEquity;
use Doraemon\model\DesignerTeam;
use Doraemon\model\KaDesigner;
use Doraemon\model\Member;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Frieza\Modules\Position\Actions\CancelApprove;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;
use Doraemon\service\User;
use Doraemon\model\Es\Designer;
use Doraemon\model\DesignerAuth;
use Beerus\Modules\DesignerList\Actions\ChangeMainArea;
use Doraemon\model\DesignerAdv;
use Doraemon\model\DesignerPackage;
use Beerus\Modules\Designer\Actions\OffLineCombo;
use Beerus\Modules\Designer\Actions\UpLineCombo;

/**
 * 设计师套餐管理
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     hujingdong  <hujingdong@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class ComboController extends Controller
{
    const PAGE_SIZE = 20;


    const PACKAGE_STATUS = [
        1 => '上线',
        2 => '下线',
    ];

    const TYPE = [
        'y' => '年费套餐',
        'm' => '季费套餐',
        'f' => '免费套餐',
        'e' => '体验套餐',
    ];

    const PACKAGE_TYPE = [
        1 => '个人',
        2 => '机构',
    ];

    const ENV = [
        0 => '非线上',
        1 => '线上'
    ];


    public function index(Request $request, Content $content)
    {

        $request_data = $request->all();
        $page_size = $request->input('per_page', self::PAGE_SIZE);
        $page = $request->input('page', 1);

        $condition['ORDER'] = ['id' => 'ASC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];

        if (!empty($request_data['id'])) {
            $condition['id'] = $request_data['id'];
        }
        $combo_info = DesignerPackage::listByCondition($condition);
        //dd($adv_info);

        $total = DesignerPackage::count('*');
        $lists = $combo_info;

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->tools(function ($tools) {
                    $url = url('/admin/Designer/combo/create');
                    $button = '<div class="btn-group download" style="margin-right: 10px">
    <a class="btn btn-sm btn-success" title="录入套餐" target="_blank" href="'.$url.'"><i class="fa fa-plus"></i><span class="hidden-xs">录入套餐</span></a>
</div>';
                    $tools->append($button);
                });
                $grid->setActionClass(DropdownActions::class);

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('id', '套餐id')->placeholder('请输入套餐id');
                    });
                    $filter->expand();
                });

                $grid->column('id', 'id');
                $grid->column('package_name', '套餐名称');
                $grid->column('price', '套餐价格');
                $grid->column("discount_price", '套餐优惠后价格');
                $grid->column('title', '套餐标题');
                $grid->column('desc', '套餐描述');
                $grid->column('version', '套餐版本');
                $grid->column("type", '套餐类型')->display(function () {
                    return self::TYPE[$this->type];
                });
                $grid->column("package_type", '套餐类别')->display(function () {
                    return self::PACKAGE_TYPE[$this->package_type];
                });
                $grid->column("remark", '备注');

                $grid->column("status", '套餐状态')->display(function () {
                    return self::PACKAGE_STATUS[$this->status];
                });
                $grid->column("env", '套餐适用环境')->display(function () {
                    return self::ENV[$this->env];
                });
                $grid->column("create_time", '创建时间');
                $grid->column("update_time", '修改时间');
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableView();
                    //获取相关信息
                    $id = $actions->row->id;
                    $combo_info = DesignerPackage::get('*',['id' => $id]);
                    //根据套餐状态展示不同的按钮
                    $combo_status = $combo_info['status'];
                    if ($combo_status == DesignerPackage::STATUS_NORMAL){
                        //如果上线中,展示下线操作
                        $actions->add(new OffLineCombo);
                    }elseif ($combo_status == DesignerPackage::STATUS_CLOSE){
                        //如果已经下线,展示上线操作
                        $actions->add(new UpLineCombo);
                    }

                });
            }
        );

        $tab = new Tab();
        $title = '设计师套餐';
        $tab->add($title, $gridList->render());
        return $content
            ->row($tab)
            ->header($title)
            ->breadcrumb(['text' => $title]);
    }

    public static function edit(Request $request, Content $content, $id = 0){
        $request = $request->all();

        if ($id == 0) {
            $is_create = true;
            $header = '套餐录入';
        } else {
            $is_create = false;
            $header = '编辑套餐信息';
        }

        $tab = new Tab();
        $tab->add('套餐信息', self::editAction($request, $id, $is_create));
        return $content->header($header)
            ->breadcrumb([
                'text' => '设计师套餐',
                'url' => route('combo.list')
            ], ['text' => $header])->row($tab);
    }

    public static function editAction($request, $id = 0, $is_create = false)
    {
        if ($is_create) {
            $form = new Form([
                'sort' => 1
            ]);
        } else {
            $data = DesignerPackage::get('*', ['id' => $id]);
            $form = new Form($data);
            $form->hidden('id');
        }

        $form->text('package_name', '套餐名称');
        $form->text('price', '套餐价格');
        $form->text('discount_price', '套餐优惠后价格');
        $form->text('title', '套餐标题');
        $form->text('desc', '套餐描述');
        $form->text('version', '套餐版本');
        $form->textarea('remark', '备注')->setWidth(4, 2);
        $form->select('type', '套餐类型')->setWidth(4, 2)->options(self::TYPE)->default(DesignerPackage::TYPE_YEAR);
        $form->select('package_type', '套餐类别')->setWidth(4, 2)->options(self::PACKAGE_TYPE)->default(DesignerPackage::PACKAGE_TYPE_PERSON);
        $form->select('status','套餐状态')->setWidth(4, 2)->options(self::PACKAGE_STATUS)->default(DesignerPackage::STATUS_NORMAL);
        $form->select('env','套餐适用环境')->setWidth(4, 2)->options(self::ENV)->default(DesignerPackage::ENV_OFFICIAL);
        $form->action('/admin/Designer/combo/save');

        return $form->render();
    }

    public static function editSubmit(Request $request){

        $data = [
            'package_name' => $request['package_name'],
            'price' => $request['price'],
            'discount_price' => $request['discount_price'],
            'status' => $request['status'],
            'type' => $request['type'],
            'package_type' => $request['package_type'],
            'env' => $request['env'],
            'version' => $request['version'],
            'remark' => $request['remark'],
            'title' => $request['title'],
            'desc' => $request['desc'],
        ];
        //dd($data);

        if (isset($request['id'])) {
            $data['update_time'] = date("Y-m-d H:i:s",time());
            $rs = DesignerPackage::update($data, [
                'id' => $request['id']
            ]);
        } else {
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            $rs = DesignerPackage::insert($data);
        }

        if ($rs) {
            $success = new MessageBag([
                'title' => '操作成功'
            ]);

            return redirect('/admin/Designer/combo/list')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '操作失败'
            ]);

            return back()->with(compact('error'));
        }
    }

}
